package com.asset.dispoal.poi;

public class NumberToChinese {

    private static final String[] CN_NUMS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] CN_UNITS = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万"};

    public static String toChineseNumber(int number) {
        if (number < 0) {
            return "无效的数字";
        } else if (number == 0) {
            return CN_NUMS[0];
        } else {
            StringBuilder sb = new StringBuilder();
            int unitIndex = 0;
            while (number > 0) {
                int digit = number % 10;
                if (digit != 0) {
                    sb.insert(0, CN_NUMS[digit] + CN_UNITS[unitIndex]);
                } else if (sb.length() > 0 && !sb.toString().startsWith(CN_NUMS[0])) {
                    sb.insert(0, CN_NUMS[0]);
                }
                number /= 10;
                unitIndex++;
            }
            return sb.toString();
        }
    }
}
